<?php

// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------


namespace app\common\controller;


use app\admin\service\ConfigService;
use app\BaseController;
use app\common\constants\AdminConstant;
use app\common\service\AuthService;
use EasyAdmin\tool\CommonTool;
use think\facade\Env;
use think\facade\View;
use think\Model;

/**
 * Class IndexController
 * @package app\common\controller
 */
class IndexController extends BaseController
{
    
    use \app\common\traits\JumpTrait;
    
    /**
     * 当前模型
     * @Model
     * @var object
     */
    protected $model;
    
    /**
     * 字段排序
     * @var array
     */
    protected $sort = [
        'id' => 'desc',
    ];
    
    /**
     * 允许修改的字段
     * @var array
     */
    protected $allowModifyFields = [
        'status',
        'sort',
        'remark',
        'is_delete',
        'is_auth',
        'title',
    ];
    
    /**
     * 不导出的字段信息
     * @var array
     */
    protected $noExportFields = ['delete_time', 'update_time'];
    
    /**
     * 下拉选择条件
     * @var array
     */
    protected $selectWhere = [];
    
    /**
     * 是否关联查询
     * @var bool
     */
    protected $relationSearch = false;
    
    /**
     * 模板布局, false取消
     * @var string|bool
     */
    protected $layout = 'layout/default';
    
    /**
     * 是否为演示环境
     * @var bool
     */
    protected $isDemo = false;
    
    
    /**
     * 初始化方法
     */
    protected function initialize()
    {
        parent::initialize();
    }
    
    /**
     * 模板变量赋值
     * @param string|array $name 模板变量
     * @param mixed $value 变量值
     * @return mixed
     */
    public function assign($name, $value = null)
    {
        return $this->app->view->assign($name, $value);
    }
    
    
    /**
     * 校验签名
     * @param $param
     * @param $appKey
     * @param $sign
     * @return bool
     */
    public function checkSign($param, $appKey, $sign)
    {
        ksort($param);
        $str = '';
        foreach ($param as $k => $v) {
            //过滤签名
            if ($k == 'sn') {
                continue;
            }
            $str .= $k . '=' . $v . '&';
        }
        //dump($param);die;
        $str = rtrim($str, '&') . $appKey;
        if ($sign != strtoupper(md5($str))) {
            $data = [
                'md5_str' =>  $str,
                'app_key' => $appKey,
                'vue_sign' => $sign
            ];
            _outPut(203, $data);
            return false;
        }
        return true;
    }
    
    /**
     * 校验token 用于和考勤机对接
     * @param $param
     * @param $appKey
     * @param $token
     * @return bool
     */
    public function checkToken($param, $appKey, $token)
    {
        ksort($param);
        $str = '';
        foreach ($param as $k => $v) {
            //过滤签名
            if ($k == 'sn') {
                continue;
            }
            $str .= $k . '=' . $v . '&';
        }
        $str = rtrim($str, '&') . $appKey;
        if ($token != strtoupper(md5($str))) {
            return false;
        }
        return true;
    }
    
    /**
     * 解析sdk传参
     * @param string $method
     * @param bool $need_return
     * @return bool|mixed
     */
    public function parseData($method = 'POST', $need_return = false)
    {
        if ($method == 'POST') {
            $data = isset($_POST['data']) ? trim($_POST['data']) : ''; //编码参数
        } else if ($method == 'GET') {
            $data = isset($_GET['data']) ? trim($_GET['data']) : '';
        }
        if (empty($data)) {
            if (!$need_return) {
                _outPut(201, '', '无data');
            } else {
                return false;
            }
        }
        $data_json = base64_decode($data);
        if ($data_json === false) {
            if (!$need_return) {
                _outPut(201, '', 'base64错误');
            } else {
                return false;
            }
        }
        $req = json_decode($data_json, true);
        if (empty($req)) {
            if (!$need_return) {
                _outPut(201, '', 'json_decode错误');
            } else {
                return false;
            }
        }
        return $req;
    }
    
    /**
     * 外部调用
     * @param $appkey
     * @param string $method
     * @return bool|mixed
     */
    public function run($appkey = WEB_KEY, $method = 'POST')
    {
        //测试环境，传递参数可以跳过验证
        if ( (true === (bool) (request()->param('isDebug')))) {
            return request()->param();
        }
        //解析参数
        $data = $this->parseData($method);
        //校验请求
        $this->checkMethod($method);
        //参数
        $signature = isset($data['sn']) ? trim($data['sn']) : '';
        if (empty($signature)) {
            _outPut(201, '', '无签名');
        }
        //校验签名
        
        if ($this->checkSign($data, $appkey, $signature) === false) {
            _outPut(203);
        }
        
        
        //校验成功返回数据
        return $data;
    }
    /**
     * 校验请求方式
     * @param string $method
     * @param bool $need_return
     * @return bool
     */
    public function checkMethod($method = 'POST', $need_return = false)
    {
        if ($_SERVER['REQUEST_METHOD'] != $method) {
            if (!$need_return) {
                _outPut(202);
            } else {
                return false;
            }
        }
        if ($need_return) {
            return true;
        }
    }

}
